package com.af.monitorModule.controller.server;

import com.af.monitorModule.common.ResultEnum;
import com.af.monitorModule.generator.entity.Monitor;
import com.af.monitorModule.generator.entity.Server;
import com.af.monitorModule.generator.service.ServerService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/server")
public class ServerController {

    @Autowired
    private ServerService serverService;

    @PostMapping("/delete")
    public JSONObject deleteOne(@RequestBody JSONObject object) {
        return serverService.deleteOne(object);
    }

    @PostMapping("/update/{id}")
    public JSONObject update(@RequestBody Server server, @PathVariable String id) {
        return serverService.updateServer(server, id);
    }

    @PostMapping("/batchStop")
    public JSONObject batchStop(@RequestBody JSONObject ids){
        return serverService.batchDelete(ids);
    }

    @PostMapping("/add")
    public JSONObject save(@RequestBody Server server){
        if (server == null){
            return ResultEnum.CREATE_ERROR.getResult();
        }
        server.setState(1);
        if (serverService.save(server)){
            return ResultEnum.OK.getResult();
        }else {
            return ResultEnum.CREATE_ERROR.getResult();
        }
    }

}
